<template>
  <el-dialog
    v-el-drag-dialog
    title="新增安检项"
    :visible.sync="visible"
    width="500px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="left" label-width="80px">
      <el-form-item label-width="80" style="width: 100%;" label="安检项名称:" prop="itemName">
        <el-input v-model="temp.itemName" placeholder="请输入安检项目名称" />
      </el-form-item>
      <el-form-item label-width="80" label="安检项类型:" prop="itemType" style="width: 100%;">
        <el-select v-model="temp.itemType" placeholder="请选择">
          <el-option
            v-for="(i, ind) in napeTypeList"
            :key="ind"
            :label="i.desc"
            :value="i.desc"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="80" label="客户类型:" prop="csmType" style="width: 100%;">
        <el-select v-model="temp.csmType" placeholder="请选择">
          <el-option
            v-for="(i, ind) in entTypeList"
            :key="ind"
            :label="i.desc"
            :value="i.desc"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryCsmTypeMap, checkNapeConfigSave, checkNapeConfigUpdate, queryMap } from '@/api'
  import { formatColumnMap } from '@/utils'

  export default {
    name: 'add',
    data() {
      return {
        visible: false,
        napeTypeList: [],
        entTypeList: [],
        params: {},
        type: '',
        temp: {
          itemName: '',
          csmType: '',
          itemId: '',
          itemType: ''
        },
        rules: {
          itemName: [{ required: true, message: '请输入安检项目名称', trigger: 'change' }],
          itemType: [{ required: true, message: '请选择安检项', trigger: 'change' }],
          csmType: [{ required: true, message: '请选择客户类型', trigger: 'change' }]
        }
      }
    },
    async created() {
      await this.getEntType()
      await this.getCheckType()
    },
    methods: {
      open(val, type) {
        this.type = type
        if (type === 2) {
          this.temp = {
            itemName: val.itemName,
            csmType: val.csmType && formatColumnMap(this.entTypeList, 'value', val.csmType, 'desc'),
            itemType: val.itemType && formatColumnMap(this.napeTypeList, 'value', val.itemType, 'desc')
          }
        }
        this.temp.itemId = val.itemId
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          this.add()
        })
      },
      // 1新增 | 2编辑
      async add() {
        this.params.itemName = this.temp.itemName
        this.params.itemId = this.temp.itemId
        this.params.csmType = formatColumnMap(this.entTypeList, 'desc', this.temp.csmType, 'value')
        this.params.itemType = formatColumnMap(this.napeTypeList, 'desc', this.temp.itemType, 'value')
        // console.log('this.params', this.params)
        const res = this.type === 1 ? await checkNapeConfigSave(this.params) : await checkNapeConfigUpdate(this.params)
        console.log('.....res', res)
        this.visible = false
        this.$notify({
          title: '提示',
          message: `${this.type === 1 ? '新增' : '编辑'}成功`,
          type: 'success',
          duration: 2000
        })
        this.$emit('addUpDataTree')
      },
      // 客户类型
      async getEntType() {
        const res = await queryCsmTypeMap()
        this.entTypeList = res.data
      },
      // 安检项类型
      async getCheckType() {
        const res = await queryMap({
          enumName: 'GasCheckItemType'
        })
        this.napeTypeList = res.data
      },
      reset() {
        this.params = {}
        this.temp = {
          itemName: '',
          csmType: '',
          itemType: ''
        }
        this.$nextTick(() => {
          // 清楚校验
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
}
</style>
